package com;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

class NumberView extends JFrame {


    JMenuBar menuBar;
    JMenu menu1;
    JMenu menu2;
    JMenuItem menuItem1;
    JMenuItem menuItem2;
    JMenuItem menuItem3;
    JMenuItem menuItem4;


    JTextField textField1,textField2,textField3,textField4;
    //创建下拉列表fuHao
    JComboBox<String> fuHao;

    JTextArea textArea,textArea1;

    JButton button;
    //监视   事件的监视器
    OperatorListener operator;
    //监视   事件的监视器
    ComputerListener computer;

    public NumberView(String s){
        super(s);
        init();
        //设置窗口可见，默认窗口为不可见为false
        setVisible(true);
        //设置关闭窗口
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    void JiaJianChengChu(){
        //        //设置布局，固定语句
        setLayout(new FlowLayout());
        //设置字体样式
        Font font = new Font("宋体",Font.BOLD,22);
        //实例化对象，并设置为设置的样式
        textField1 = new JTextField(10);
        textField2 = new JTextField(10);
        textField1.setFont(font);
        textField2.setFont(font);
        fuHao = new JComboBox<>();
        fuHao.setFont(font);
        button = new JButton("计算");
        button.setFont(font);
        fuHao.addItem("选择运算符号");
        //添加下拉列表内容
        String[] a = {"+","-","*","/"};
        for(int i=0;i<a.length;i++){
            fuHao.addItem(a[i]);
        }
        //默认为-1，不显示任何的符号
        fuHao.setSelectedIndex(-1);
        //设置窗格，放置计算结果
        textArea = new JTextArea(9,30);
        textArea.setFont(font);
        //实例化监视器
        operator = new OperatorListener();
        computer = new ComputerListener();
        //将当前的窗口传递给Operator组合的窗口
        operator.setNumberView(this);
        //将当前的窗口传递给Computer组合的窗口
        computer.setNumberView(this);
        //operator是监视器
        fuHao.addItemListener(operator);
        //operator是监视器
        fuHao.addActionListener(operator);
        //computer是监视器
        button.addActionListener(computer);
        //设置布局，将文本框，下拉列表，按钮，窗格添加到窗口
        add(textField1);
        add(fuHao);
        add(textField2);
        add(button);
        add(new JScrollPane(textArea));

    }

    void ShuCheng(){
        setLayout(new FlowLayout());
        JLabel label = new JLabel("数字");
        textField3 = new JTextField(10);
        JLabel label1 = new JLabel("结果");
        textField4 = new JTextField(10);
        JButton button = new JButton("计算");
        ShuChengListener shuChengListener = new ShuChengListener();
        shuChengListener.setNumberView(this);
        button.addActionListener(shuChengListener);
        add(label);
        add(textField3);
        add(button);
        add(label1);
        add(textField4);


    }

    void init(){

        menuBar = new JMenuBar();
        menu1 = new JMenu("计算");
        menu2 = new JMenu("帮助");
        menuItem1 = new JMenuItem("加减乘除");
        menuItem2 = new JMenuItem("数乘");
        menuItem3 = new JMenuItem("设置1");
        menuItem4 = new JMenuItem("设置2");

        menu1.add(menuItem1);
        menu1.addSeparator();
        menu1.add(menuItem2);
        menu2.add(menuItem3);
        menu2.add(menuItem4);
        menuBar.add(menu1);
        menuBar.add(menu2);
        setJMenuBar(menuBar);

        Menu1Listener menu1Listener = new Menu1Listener();
        menu1Listener.setNumberView(this);
        menuItem1.addActionListener(menu1Listener);

        Menu2Listener menu2Listener = new Menu2Listener();
        menu2Listener.setNumberView(this);
        menuItem2.addActionListener(menu2Listener);
    }

}

class Menu1Listener implements ActionListener{
    NumberView numberView;
    public void setNumberView(NumberView numberView) {
        this.numberView = numberView;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        numberView.JiaJianChengChu();
    }
}

class Menu2Listener implements ActionListener{
    NumberView numberView;
    public void setNumberView(NumberView numberView) {
        this.numberView = numberView;
    }
    @Override
    public void actionPerformed(ActionEvent e) { numberView.ShuCheng(); }
}

class ComputerListener implements ActionListener{

    NumberView numberView;
    String fuHao;
    //用于注册监视器
    public void setNumberView(NumberView numberView) {
        this.numberView = numberView;
    }

    public void setFuHao(String fuHao) {
        this.fuHao = fuHao;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            double number1 = Double.parseDouble(numberView.textField1.getText());
            double number2 = Double.parseDouble(numberView.textField2.getText());
            double result = 0;
            boolean b = true;
            if(fuHao.equals("+"))
                result = number1+number2;
            else if(fuHao.equals("-"))
                result = number1-number2;
            else if(fuHao.equals("*"))
                result = number1*number2;
            else if(fuHao.equals("/"))
                result = number1/number2;
            else
                b = false;
            if(b)
                numberView.textArea.append(number1+" "+fuHao+" "+number2+" "+"="+" "+result);
        } catch (NumberFormatException ex) {
            numberView.textArea.append("\n请输入数字字符\n");
        }
    }
}

class OperatorListener implements ItemListener, ActionListener{

    NumberView numberView;

    public void setNumberView(NumberView numberView) {
        this.numberView = numberView;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //获取选择的符号
        String fuHao = numberView.fuHao.getSelectedItem().toString();
        //将符号传递给窗口下的computer对象，computer对象监视button，启动button也就启动了计算程序
        numberView.computer.setFuHao(fuHao);
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        String fuHao = numberView.fuHao.getSelectedItem().toString();
        numberView.computer.setFuHao(fuHao);
    }
}

class ShuChengListener implements ActionListener{

    NumberView numberView;

    public void setNumberView(NumberView numberView) {
        this.numberView = numberView;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            int n = Integer.parseInt(numberView.textField3.getText());
            int m = 1;
            for(int i=1;i<=n;i++){
                m*=i;
            }
            numberView.textField4.setText(String.valueOf(m));
        } catch (NumberFormatException ex) {
            numberView.textField4.setText("\n请输入数字字符\n");
        }
    }
}

public class test {

    public static void main(String[] args) {
        NumberView numberView = new NumberView("计算器");
        numberView.setBounds(100,100,600,360);

    }

}
